from flask import Flask,jsonify,render_template,request,escape
import requests,json
import pandas as pd
import cufflinks as cf
import plotly as py
import plotly.graph_objs as go
import io
import base64
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pyecharts import options as opts
from pyecharts.charts import Bar
from jinja2 import Markup
from matplotlib import rcParams
rcParams['font.family'] = 'simhei'



app = Flask(__name__)

df = pd.read_excel("中国进出口总额年度数据.xls", encoding = "utf8", sep="\t")
# df_show = df.head(242)

recent_year = df.iloc[15:20]
recent_year

out = recent_year[['指标','进出口总额(人民币)(亿元)','出口总额(人民币)(亿元)','进口总额(人民币)(亿元)','进出口差额(人民币)(亿元)']]
DA = out.reset_index()






y1=DA['进口总额(人民币)(亿元)']
x1=DA['指标']
x2=DA['指标']
y2=DA['出口总额(人民币)(亿元)']
plt.plot(x1,y1,label='进口',linewidth=3,color='r',marker='o',
markerfacecolor='blue',markersize=12)
plt.plot(x2,y2,label='出口')
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('进口总额/出口总额')
plt.legend()
# plt.show()

				

@app.route("/",methods = ["GET"])
def base() :
	data_str = df.to_html()
	data_str1 = DA.to_html()
	return render_template("base.html",
							the_res = data_str,
							the_res1 = data_str1)




def bar_base1() -> Bar:
    tp1 = (
        Bar()
            .add_xaxis(["2016年", "2017年", "2018年", "2019年", "2020年"])
            .add_yaxis("进口(显示）", [104967.17, 124789.81, 140880.32, 143253.69, 142231.00])
            .add_yaxis("出口（显示）", [138419.29, 153309.43, 164127.81, 172373.63, 179326.00])
            .set_global_opts(title_opts=opts.TitleOpts(title="近年进出口情况", subtitle="（人民币/亿元）"))
    )
    return tp1

@app.route("/tp1")
def tp1():
	c = bar_base1()
	return Markup(c.render_embed())

def bar_base2() -> Bar:
    tp2 = (
        Bar()
            .add_xaxis(["2016年", "2017年", "2018年", "2019年", "2020年"])
            .add_yaxis("总额（显示）", [243386.46, 278099.24	, 305008.13, 315627.32, 321557.00])
            .add_yaxis("差额（显示）", [33452.12, 28519.62, 23247.49	, 29119.94, 37096.00])
            .set_global_opts(title_opts=opts.TitleOpts(title="进出口总额/差额", subtitle="（人民币/亿元）"))
    )
    return tp2

@app.route("/tp2")
def tp2():
	c = bar_base2()
	return Markup(c.render_embed())

if __name__ == '__main__':
	app.run(debug=True)
	